package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.Permission;
import com.itheima.pojo.Role;
import com.itheima.service.RoleService;
import org.apache.ibatis.annotations.ResultMap;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/role")
public class RoleController {
    @Reference
    private RoleService roleService;

    @RequestMapping("/add")
    public Result add(@RequestBody Role role,Integer[] permissionIds,Integer[] menuIds){
        try {
            roleService.add(role,permissionIds,menuIds);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "新增角色失败");
        }
        return new Result(true,"新增角色成功");
    }

    @RequestMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean){
        return roleService.pageQuery(queryPageBean);
    }

    @RequestMapping("/findById")
    public Result findById(Integer id){
        try {
            Role role = roleService.findById(id);
            return new Result(true,"查询角色信息成功",role);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"查询角色信息失败");
        }
    }

    @RequestMapping("/findPermissionIdsByRoleId")
    public Result findPermissionIdsByRoleId(Integer id){
        try {
            List<Integer> permissionIds = roleService.findPermissionIdsByRoleId(id);
            return new Result(true,"查询权限成功",permissionIds);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"查询权限失败");
        }
    }

    @RequestMapping("/findMenuIdsByRoleId")
    public Result findMenuIdsByRoleId(Integer id){
        try {
            List<Integer> menuIds = roleService.findMenuIdsByRoleId(id);
            return new Result(true,"查询菜单成功",menuIds);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"查询菜单失败");
        }
    }

    @RequestMapping("/edit")
    public Result edit(@RequestBody Role role,Integer[] permissionIds,Integer[] menuIds){
        try {
            roleService.edit(role,permissionIds,menuIds);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"编辑角色失败");
        }
        return new Result(true,"编辑角色成功");
    }

    @PreAuthorize("hasAuthority('ROLE_DELETE')")
    @RequestMapping("/delete")
    public Result delete(Integer id){
        try {
            roleService.deleteById(id);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"删除角色失败");
        }
        return new Result(true,"删除角色成功");
    }

    @RequestMapping("/findAll")
    public Result findAll(){
        try {
            List<Role> list = roleService.findAll();
            return new Result(true,"查询角色成功",list);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"查询角色失败");
        }
    }
}
